package ferp.android.dialogs;

import android.content.Context;
import android.view.View;
import android.widget.ListView;
import ferp.android.R;

/**
 * User: olegs
 * Date: 30/05/12 23:28
 */
public class CheckListViewDialog extends ChoiceDialog
{
  public interface Listener
  {
    void onSaveClick(int requestCode, boolean[] choices);
  }

  public CheckListViewDialog(Context context, final Listener listener, final int requestCode)
  {
    super(context);

    resolveButton(R.id.dlg_element_footer_button, R.string.save).setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View view)
      {
        for (int i = 0; i < ld.list.getCount(); ++i)
          choices[i] = ld.list.isItemChecked(i);

        listener.onSaveClick(requestCode, choices);
      }
    });
  }

  public void set(String title, String[] options, boolean[] selected)
  {
    this.title.setText(title);

    // create choices array
    choices = new boolean[options.length];

    // reset options
    ld.set(options);

    // set clicked items
    for (int i = 0; i < selected.length; ++i)
      ld.list.setItemChecked(i, selected[i]);
  }

  @Override
  protected int getMode() {return ListView.CHOICE_MODE_MULTIPLE;}

  @Override
  protected int getAdapterLayoutResource() {return android.R.layout.simple_list_item_multiple_choice;}

  @Override
  protected int getFooterVisibility() {return View.VISIBLE;}

  // choices array
  private boolean[] choices;
}
